package view;

import java.awt.BorderLayout;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import controller.FirewallSession;

/**
 * Class is responsible for status bar.
 * When frame is registered for the status bar, new label is created under the frame with FW characteristics. 
 */
public class StatusBar
{
	/**
	 * Flag that FW is turned on
	 */
	protected static boolean fwTurnedOn = true;
	
	/**
	 * Flag that FW is writable
	 */
	protected static boolean isWrite = true;
	
	/**
	 * Flag that logger is turned on
	 */
	protected static boolean loggerOn = true;

	/**
	 * List of the labels for frames that registered for the status bar option
	 */
	private static ArrayList<JLabel> statusBar = new ArrayList<JLabel>();
	
	/**
	 * Current shown status bar
	 */	
	private static StatusBar currentStatusBar = null;
	
	/**
	 * server controller
	 */
	private static FirewallSession ui = null;
	
	/**
	 * C'tor - status bar controller is defined.
	 * @param ui	- the controller for the system actions
	 */
	private StatusBar(FirewallSession UI)
	{
		ui = UI;
	}
	
	/**
	 * Returns current status bar if exists, else creates it.
	 * @return status bar
	 */
	public static StatusBar getInstance(FirewallSession UI)
	{
		if(currentStatusBar == null)
		{
			currentStatusBar = new StatusBar(UI);
		}
		
		return currentStatusBar;
	}
	
	/**
	 * Creates the label for the registering frame and adds the relevant label to the list.
	 * @param frame - the frame for the status bar
	 */
	public static void register(JFrame frame)
	{	
		/* create the label */
		JLabel status = new JLabel(toStatusString());
		statusBar.add(status);
		
		/* create new pannel for the status bar and add it to the frame */
		JPanel newPanel = new JPanel();
		newPanel.setLayout(new BorderLayout());
		
		newPanel.add(frame.getContentPane(), BorderLayout.NORTH);
		newPanel.add(status, BorderLayout.SOUTH);
		
		frame.setContentPane(newPanel);
	}
	
	/**
	 * Updates all labels in the list according to the new FW status.
	 */
	public static void update()
	{
		String newText = toStatusString();
		
		Iterator it = statusBar.iterator();
		while(it.hasNext())
		{
			JLabel label = (JLabel)it.next();
			label.setText(newText);
		}
	}
	
	/**
	 * @return the string with FW status 
	 */
	private static String toStatusString()
	{
		/* FW turned on/off */
		String FWstatus = null;
		if(fwTurnedOn)
		{
			FWstatus = "turned on";
		}
		else
		{
			FWstatus = "turned off";
		}
	
		/* FW writable/read only */
		String writeStatus = null;
		if(isWrite)
		{
			writeStatus = "writable";
		}
		else
		{
			writeStatus = "read only";
		}

		/* Logger turned on/off */
		String loggerStatus = null;
		if(loggerOn)
		{
			loggerStatus = "logger on";
		}
		else
		{
			loggerStatus = "logger off";
		}
		
		/* User mode standart/advanced*/
		String userMode = Settings.userMode.toString();
		
		String ruleset = "";
		try {
			ruleset = "Active ruleset: " + ui.getCurrentRuleSet();
		} catch (RemoteException e) {
			ErrorFrame.AddError("Connection problems: active ruleset is uknown", null);
		}
		
		/* Build the string */
		return "FW: " + FWstatus + " | " + writeStatus + " | " + loggerStatus +
				"     User mode: " + userMode + "     " + ruleset;
	}
}
